﻿#include "SetRate.h"
#include "ui_setrate.h"

SetRate::SetRate(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetRate)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");
    QRegExp regExpinput("^\\d{1,2}\\.\\d{4}$");
    QValidator* validatorregExpinput = new QRegExpValidator(regExpinput, 0);
    ui->inputShowLineEdit->setValidator(validatorregExpinput);
    page = 0;
    connect(ui->buttonBack, SIGNAL(clicked()), this, SLOT(on_buttonBack_clicked()));
    connect(ui->buttonPrev, SIGNAL(clicked()), this, SLOT(on_buttonPrev_clicked()));
    connect(ui->buttonNext, SIGNAL(clicked()), this, SLOT(on_buttonNext_clicked()));
    connect(ui->buttonSave, SIGNAL(clicked()), this, SLOT(on_buttonSave_clicked()));
    keyBoardConnect();
    ui->labelHint->hide();
    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    ui->tableWidget->setColumnWidth(0,50);
    ui->tableWidget->setColumnWidth(1,90);
    ui->tableWidget->setColumnWidth(4,50);
    ui->tableWidget->setColumnWidth(5,90);

    registe = new db_register;
    registe->db_tran_type = TABLE_XJ;
    registe->db_card_type = TABLE_XJ;
    registe->db_err_type  = TABLE_XJ;
    protocol_database_register(registe);

    price_model = new xj_price_model;
    if(ERROR_OK == protocol_get_PriceModel(price_model)){
        qDebug()<<"获取成功";
    }

    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(RateShow(int)));
}

SetRate::~SetRate()
{
    delete ui;
    delete price_model;
}

void SetRate::RateShow(int current)
{
    char  buff[64];

    if(current == SetRatePage){
        page = 0;
        memset(price_model,0,sizeof(price_model));
        if(ERROR_OK == protocol_get_PriceModel(price_model)){
            qDebug()<<"获取成功price_model";

            for(int i=0; i<48; i++){
                qDebug()<< "power rate[" << i << "]:" <<price_model->power_rate[i];
                qDebug()<< "service rate[" << i << "]:" <<price_model->service_rate[i];
            }
        }

        if (page == 0) {
            ui->buttonPrev->setEnabled(false);
            ui->buttonNext->setEnabled(true);
        }

        for (int i=0; i<2; i++) {
            for(int j=0; j<6; j++){
                item[0][j+(i*6)] = new QTableWidgetItem(QString::number(j+(i*6)+1));
                item[0][j+(i*6)]->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(j+1, i*4, item[0][j+(i*6)]);
                qDebug() << item[0][j+(i*6)]->data(QTableWidgetItem::Type);

                sprintf(buff, "%d:%02d~%d:%02d", ((j+(i*6))*30)/60, (j+(i*6))%2*30, (j+(i*6)+1)*30/60,(j+(i*6)+1)%2*30);
//                item[1][j+(i*6)] = new QTableWidgetItem(intToStr(((j+(i*6))*30)/60)+":"+
//                                                        intToStr((j+(i*6))%2*30)+" ~ "+
//                                                        intToStr((j+(i*6)+1)*30/60)+":"+
//                                                        intToStr((j+(i*6)+1)%2*30));
                item[1][j+(i*6)] = new QTableWidgetItem(buff);
                item[1][j+(i*6)]->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(j+1,i*4+1,item[1][j+(i*6)]);
                qDebug() << item[1][j+(i*6)]->data(QTableWidgetItem::Type);

                sprintf(buff, "%.4f", price_model->power_rate[j+(i*6)]*1.0/10000);
                item[2][j+(i*6)] = new QTableWidgetItem(buff);
                item[2][j+(i*6)]->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(j+1,i*4+2,item[2][j+(i*6)]);
                qDebug() << item[2][j+(i*6)]->data(QTableWidgetItem::Type);

                sprintf(buff, "%.4f", price_model->service_rate[j+(i*6)]*1.0/10000);
                item[3][j+(i*6)] = new QTableWidgetItem(buff);
                item[3][j+(i*6)]->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(j+1,i*4+3,item[3][j+(i*6)]);
                qDebug() << item[3][j+(i*6)]->data(QTableWidgetItem::Type);

            }
        }
    }
}

void SetRate::on_buttonBack_clicked()
{
    stackLayout->setCurrentIndex(SystemSetting4Page);
    page = 0;
}

void SetRate::on_buttonPrev_clicked()
{
    char  buff[64];

    if ((page > 0) && (page <= 3))
    {
        page--;
    }
    else
    {
        page = 3;
    }

    if(page == 0){
        ui->buttonPrev->setEnabled(false);
//        ui->buttonNext->setEnabled(true);
    }else{
        ui->buttonNext->setEnabled(true);
    }
    for(int i=0; i<2; i++){
        for(int j=0; j<6; j++){
            item[0][j+(i*6)+page*12] = new QTableWidgetItem(QString::number(j+(i*6)+page*12+1));
            item[0][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4,item[0][j+(i*6)+page*12]);

//            item[1][j+(i*6)+page*12] = new QTableWidgetItem(intToStr(((j+(i*6)+page*12)*30)/60)+":"+
//                                                    intToStr((j+(i*6)+page*12)%2*30)+"~"+
//                                                    intToStr((j+(i*6+page*12)+1)*30/60)+":"+
//                                                    intToStr((j+(i*6+page*12)+1)%2*30));
            sprintf(buff, "%d:%02d~%d:%02d", ((j+(i*6)+page*12)*30)/60, (j+(i*6)+page*12)%2*30, (j+(i*6+page*12)+1)*30/60,(j+(i*6+page*12)+1)%2*30);
            item[1][j+(i*6)+page*12] = new QTableWidgetItem(buff);
            item[1][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4+1,item[1][j+(i*6)+page*12]);

//            item[2][j+(i*6)+page*12] = new QTableWidgetItem(QString::number(price_model->power_rate[j+(i*6)+page*12]*1.0/10000, 'f', 4));
            sprintf(buff, "%.4f", price_model->power_rate[j+(i*6)+page*12]*1.0/10000);
            item[2][j+(i*6)+page*12] = new QTableWidgetItem(buff);
            item[2][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4+2,item[2][j+(i*6)+page*12]);

//            item[3][j+(i*6)+page*12] = new QTableWidgetItem(QString::number(price_model->service_rate[j+(i*6)+page*12]*1.0/10000, 'f', 4));
            sprintf(buff, "%.4f", price_model->service_rate[j+(i*6)+page*12]*1.0/10000);
            item[3][j+(i*6)+page*12] = new QTableWidgetItem(buff);
            item[3][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4+3,item[3][j+(i*6)+page*12]);

        }
    }
    qDebug()<<page;

}

void SetRate::on_buttonNext_clicked()
{
    char  buff[64];

    if ((page >= 0) && (page < 3))
    {
        page++;
    }
    else
    {
        page = 0;
    }

    if(page == 3){
        ui->buttonNext->setEnabled(false);
//        ui->buttonPrev->setEnabled(true);
    }else{
        ui->buttonPrev->setEnabled(true);
    }
    qDebug()<<page;
    for(int i=0; i<2; i++){
        for(int j=0; j<6; j++){
            item[0][j+(i*6)+page*12] = new QTableWidgetItem(QString::number(j+(i*6)+page*12+1));
            item[0][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4,item[0][j+(i*6)+page*12]);

//            item[1][j+(i*6)+page*12] = new QTableWidgetItem(intToStr(((j+(i*6)+page*12)*30)/60)+":"+
//                                                    intToStr((j+(i*6)+page*12)%2*30)+" ~ "+
//                                                    intToStr((j+(i*6+page*12)+1)*30/60)+":"+
//                                                    intToStr((j+(i*6+page*12)+1)%2*30));
            sprintf(buff, "%d:%02d~%d:%02d", ((j+(i*6)+page*12)*30)/60, (j+(i*6)+page*12)%2*30, (j+(i*6+page*12)+1)*30/60,(j+(i*6+page*12)+1)%2*30);
            item[1][j+(i*6)+page*12] = new QTableWidgetItem(buff);
            item[1][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4+1,item[1][j+(i*6)+page*12]);

//            item[2][j+(i*6)+page*12] = new QTableWidgetItem(QString::number(price_model->power_rate[j+(i*6)+page*12]*1.0/10000, 'f', 4));
            sprintf(buff, "%.4f", price_model->power_rate[j+(i*6)+page*12]*1.0/10000);
            item[2][j+(i*6)+page*12] = new QTableWidgetItem(buff);
            item[2][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4+2,item[2][j+(i*6)+page*12]);

//            item[3][j+(i*6)+page*12] = new QTableWidgetItem(QString::number(price_model->service_rate[j+(i*6)+page*12]*1.0/10000, 'f', 4));
            sprintf(buff, "%.4f", price_model->service_rate[j+(i*6)+page*12]*1.0/10000);
            item[3][j+(i*6)+page*12] = new QTableWidgetItem(buff);
            item[3][j+(i*6)+page*12]->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(j+1,i*4+3,item[3][j+(i*6)+page*12]);
        }
    }
}
QString SetRate::intToStr(int numtime)
{
    if(numtime > 9){
        return QString::number(numtime, 'g', 2);
    }
    return QString("0" + QString::number(numtime));
}

void SetRate::on_buttonSave_clicked()
{
    if(ERROR_OK == protocol_update_PriceModel(price_model)){
            qDebug()<<"更新成功";
            ui->labelHint->setText(tr("设置成功"));
            ui->labelHint->show();
            QTimer::singleShot(3000,ui->labelHint,SLOT(hide()));//显示三秒后隐藏
    }else{
        ui->labelHint->setText(tr("设置失败"));
        ui->labelHint->show();
        QTimer::singleShot(3000,ui->labelHint,SLOT(hide()));//显示三秒后隐藏
    }
}
void SetRate::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->input, SIGNAL(clicked()), this, SLOT(inputTableWidget()));
}
void SetRate::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());
    if(btn == ui->RemoveAll){
        ui->inputShowLineEdit->clear();
    }
    else if(btn == ui->Remove){
        ui->inputShowLineEdit->backspace();
    }else{
        ui->inputShowLineEdit->insert(btn->text());
    }
}
void SetRate::inputTableWidget()
{
    int temp[4] = {0,1,4,5};
    if(!ui->tableWidget->selectedItems().isEmpty()){
        for(int i=0; i<4; i++){
            if(ui->tableWidget->selectedItems().at(0)->column() == temp[i]){
                ui->inputShowLineEdit->clear();
                return;
            }
        }
        if(ui->tableWidget->selectedItems().at(0)->column() == 2){
            price_model->power_rate[ui->tableWidget->selectedItems().at(0)->row()+page*12-1] =
                    ui->inputShowLineEdit->text().toFloat()*10000;
            qDebug()<<ui->inputShowLineEdit->text().toFloat()*10000;
        }else if(ui->tableWidget->selectedItems().at(0)->column() == 6){
            price_model->power_rate[ui->tableWidget->selectedItems().at(0)->row()+5+page*12] =
                    ui->inputShowLineEdit->text().toFloat()*10000;
            qDebug()<<ui->inputShowLineEdit->text().toFloat()*10000;
        }
        if(ui->tableWidget->selectedItems().at(0)->column() == 3){
            price_model->service_rate[ui->tableWidget->selectedItems().at(0)->row()+page*12-1] =
                    ui->inputShowLineEdit->text().toFloat()*10000;
            qDebug()<<ui->inputShowLineEdit->text().toFloat()*10000;
        }else if(ui->tableWidget->selectedItems().at(0)->column() == 7){
            price_model->service_rate[ui->tableWidget->selectedItems().at(0)->row()+5+page*12] =
                    ui->inputShowLineEdit->text().toFloat()*10000;
            qDebug()<<ui->inputShowLineEdit->text().toFloat()*10000;
        }
        if(!ui->inputShowLineEdit->text().isEmpty()){
            ui->tableWidget->selectedItems().at(0)->setText(QString::number(ui->inputShowLineEdit->text().toFloat(), 'f', 4));
        }
    }
    ui->inputShowLineEdit->clear();
}
